<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of adminCountModel
 * 管理员发布内容次数控制模型
 * @author xlp
 */
class adminCountModel extends model {

    function __construct() {
        parent::__construct();
        $this->dbTable = 'admin_count';
    }

    /*
     * 根据aid获取帐号发布内容的剩余次数
     * @param $aid int AID
     * @param $groupid int 帐号组别
     * @param $type string 要检查的类型
     * @return int
     */

    public function checkAdminCounts($aid, $groupid, $type) {
        $adminConfig = C('admin', 'counts');
        if (!isset($adminConfig[$groupid]) || !isset($adminConfig[$groupid][$type])) {
            return array('status' => 0, 'count' => -2, 'msg' => '未设置');
        }
        $config = $adminConfig[$groupid];
        //如果当前组别不允许发布则立即返回
        if ($config[$type]['counts'] < -1) {
            return array('status' => 0, 'count' => -2, 'msg' => '用户组没有发布的权限');
        }
        if ($config[$type]['counts'] == -1) {
            return array('status' => 1, 'count' => -1, 'msg' => '不限');
        }
        $dataInfo = array('year' => date('Y'), 'month' => date('m'), 'day' => date('d'));
        $where = array('aid' => $aid, 'fmod' => $config[$type]['key']);
        switch ($config[$type]['type']) {
            case 'year':
                $where['a_year'] = $dataInfo['year'];
                break;
            case 'month':
                $where['a_year'] = $dataInfo['year'];
                $where['a_month'] = $dataInfo['month'];
                break;
            case 'day':
                $where['a_year'] = $dataInfo['year'];
                $where['a_month'] = $dataInfo['month'];
                $where['a_day'] = $dataInfo['day'];
                break;
        }
        $whereField = '1=1';
        foreach ($where as $k => $v) {
            $whereField.=' AND ' . $k . '=' . $v;
        }
//        echo 'SELECT SUM(counts) AS counts FROM __TABLE__ WHERE ' . $whereField;
        $has = $this->query('SELECT SUM(counts) AS counts FROM __TABLE__ WHERE ' . $whereField . ' LIMIT 1', true);
//        z($has);
//        print_r($has);
        if (isHave($has['counts'])) {
            $surplus = $config[$type]['counts'] - $has['counts'];
        } else {
            $surplus = $config[$type]['counts'];
        }
        //返回剩余的次数
        if ($surplus > 0) {
            return array('status' => 1, 'count' => $surplus, 'msg' => $surplus);
        } else {
            return array('status' => 0, 'count' => 0, 'msg' => '发布次数已达上限');
        }
    }

    /*
     * 增加发布的记录信息
     * @param $aid int AID
     * @param $groupid int 帐号组别
     * @param $type string 要检查的类型
     * @return int
     */

    public function addAdminCount($aid, $groupid, $type) {
        $adminConfig = C('admin', 'counts');
        if (!isset($adminConfig[$groupid]) || !isset($adminConfig[$groupid][$type])) {
            return 0;
        }
        $dataInfo = array('year' => date('Y'), 'month' => date('m'), 'day' => date('d'));
        $where = array('aid' => $aid, 'fmod' => $adminConfig[$groupid][$type]['key'], 'a_year' => $dataInfo['year'], 'a_month' => $dataInfo['month'], 'a_day' => $dataInfo['day']);
        $has = $this->field('aid')->where($where)->find();
        if ($has) {
            $this->where($where)->setInc('counts', 1);
        } else {
            $where['counts'] = 1;
            $this->insert($where);
        }
        return true;
    }

}
